 

function runBlock($log, $rootScope, $http, $state, $stateParams) {
	'ngInject';

  // 设置全局变量
  window.$state = $state; // $state
  $rootScope.$state = $state;
  $rootScope.defaultPage = 'main'; // 默认跳转页面
  $rootScope.serverIp = SERVER_IP; // 服务器地址
  $rootScope.qrCodeIp = QR_CODE_IP // 二维码地址
  $rootScope.serverContext = SERVER_CONTEXT; // 服务器path

  // 返回上一页
  $rootScope.goBack = function(){
    history.back(-1);
  }

  // 登陆/权限拦截器
  $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
    // 判断登陆状态并保存到rootScope
    $rootScope.isLogined = localStorage.getItem('userInfo') ? true : false;
    if (toState.name =='login') return;// 如果是进入登录界面则允许

    if (!$rootScope.isLogined) { // 登陆拦截器
      event.preventDefault();// 取消默认跳转行为
      $state.go("login",{from:toState.name,w:'notLogin'});//跳转到登录界面
      return false;
    } else {
      // 页面权限拦截器
      if (!$rootScope.authorizationInfo) {
        event.preventDefault();
        let userInfo = JSON.parse(localStorage.getItem('userInfo'));
        let userId = userInfo ? userInfo.userId : null;
        if (userId) {
          // 获得权限信息
          $http.get(`${SERVER_CONTEXT}/api/authority/queryAuthorityByUserId/${userId}`)
            .then((resp) => {
              $rootScope.authorizationInfo = resp.data;
              $state.go(toState.name, toParams);
            });
          } else {
            $state.go("login",{from:toState.name,w:'notLogin'});//跳转到登录界面
          }
      } else { // 根据权限信息进行拦截
        if (NODE_ENV == 'development') return;
        let toUrl = toState.url;
        let menuList = $rootScope.authorizationInfo.menuList;
        // if (!isAuthenticatedUrl(toUrl, menuList)) {
        //   event.preventDefault();
        //   layer.alert('抱歉,您无权限访问该页面');
        // }

        function isAuthenticatedUrl(toUrl, menuList) {
          for(let i=0; i<menuList.length; i++){
            let authenticatedUrl = menuList[i].url;
            if (authenticatedUrl && authenticatedUrl.indexOf(toUrl) != -1) {
              return true;
            }
          }
          return false;
        }
      }
    }

    // 保存用户信息到$rootScope
    $rootScope.userInfo = JSON.parse(localStorage.getItem('userInfo'));
  });


  // // 自动获得权限信息
  // $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
  //   debugger;
  //   if (toState.name =='login') return;// 如果是进入登录界面则允许
  //   console.log($rootScope.authorizationInfo,1);

  
    
  // });

  // // 页面权限拦截器
  // $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
  //   // if (NODE_ENV == 'development') return;
  //   debugger;
  //   if (toState.name =='login') return;// 如果是进入登录界面则允许
  //   if ($rootScope.authorizationInfo) {
  //     let toUrl = toState.url;
  //     let menuList = $rootScope.authorizationInfo.menuList;
  //     if (!isAuthenticatedUrl(toUrl, menuList)) {
  //       event.preventDefault();
  //       layer.alert('抱歉,您无权限访问该页面');
  //     }

  //     function isAuthenticatedUrl(toUrl, menuList) {
  //       for(let i=0; i<menuList.length; i++){
  //         let authenticatedUrl = menuList[i].url;
  //         if (authenticatedUrl && authenticatedUrl.indexOf(toUrl) != -1) {
  //           return true;
  //         }
  //       }
  //       return false;
  //     }
  //   } else {
  //     return false;
  //   }
  // });


  $rootScope.fileUploadUrl = {
    device: `${SERVER_CONTEXT}/api/deviceinfo/uploadDeviceInfo`,  // 设备图片上传
    deviceCheckRecord: `${SERVER_CONTEXT}/api/devicecheckrecord/uploadDeviceInfo`, // 设备检查记录文件上传
    securityMeeting: `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/upload`, // 安全会议文件上传
    technicalDisclosure: `${SERVER_CONTEXT}/api/education/uploadEducation`, // 教育培训、安全交底
    workerInfo: `${SERVER_CONTEXT}/api/safesite/workerInfo/upload`, // 人员基本信息
    officeWorkData: `${SERVER_CONTEXT}/api/safesite/officeworkdata/upload`, // 内页资料上传
    problemModify: `${SERVER_CONTEXT}/api/securityActivities/problemModify/upload`,// 问题整改图片上传
  }

  // $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
  //   console.log('$stateChangeStart to '+toState.to+'- fired when the transition begins. toState,toParams : \n',toState, toParams);
  // });
  // $rootScope.$on('$stateChangeError',function(event, toState, toParams, fromState, fromParams, error){
  //   console.log('$stateChangeError - fired when an error occurs during transition.');
  //   console.log(arguments);
  // });
  // $rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){
  //   console.log('$stateChangeSuccess to '+toState.name+'- fired once the state transition is complete.');
  // });
  // // $rootScope.$on('$viewContentLoading',function(event, viewConfig){
  // //   // runs on individual scopes, so putting it in "run" doesn't work.
  // //   console.log('$viewContentLoading - view begins loading - dom not rendered',viewConfig);
  // // });
  // $rootScope.$on('$viewContentLoaded',function(event){
  //   console.log('$viewContentLoaded - fired after dom rendered',event);
  // });
  // $rootScope.$on('$stateNotFound',function(event, unfoundState, fromState, fromParams){
  //   console.log('$stateNotFound '+unfoundState.to+'  - fired when a state cannot be found by its name.');
  //   console.log(unfoundState, fromState, fromParams);
  // });
}

export default runBlock;
